package com.example.wisdomclassroomapi.domain.repository;

import com.example.wisdomclassroomapi.domain.entity.CourseLessonEntity;
import com.example.wisdomclassroomapi.infrastructure.mapper.CourseLessonMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
 * @Package: com.example.wisdomclassroomapi.domain.repository
 * @Description: <>
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Repository
public class CourseLessonRepository {
    @Resource
    private CourseLessonMapper courseLessonMapper;

    public void add(CourseLessonEntity entity) {
       courseLessonMapper.insert(entity);
    }

    public CourseLessonEntity findById(String id) {
        return courseLessonMapper.selectById(id);
    }

    public int updateById(CourseLessonEntity entity) {
        return courseLessonMapper.updateById(entity);
    }

    public int deleteById(String id) {
        return courseLessonMapper.deleteById(id);
    }

    public IPage<CourseLessonEntity> page(CourseLessonEntity entity,IPage page) {
        QueryWrapper<CourseLessonEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getSubjectId())){
            wrapper.eq("subject_id",entity.getSubjectId());
            }
            if (StringUtils.isNotBlank(entity.getTitle())){
            wrapper.eq("title",entity.getTitle());
            }
            if (StringUtils.isNotBlank(entity.getContent())){
            wrapper.eq("content",entity.getContent());
            }
            if (StringUtils.isNotBlank(entity.getCoverUrl())){
            wrapper.eq("cover_url",entity.getCoverUrl());
            }
            if (StringUtils.isNotBlank(entity.getTeacherId())){
            wrapper.eq("teacher_id",entity.getTeacherId());
            }
            if (StringUtils.isNotBlank(entity.getType())){
            wrapper.eq("type",entity.getType());
            }
            if (entity.getLiveDate()!=null){
            wrapper.eq("live_date",entity.getLiveDate());
            }
            if (entity.getScore()!=null){
            wrapper.eq("score",entity.getScore());
            }
            if (entity.getScorePeople()!=null){
            wrapper.eq("score_people",entity.getScorePeople());
            }
        return courseLessonMapper.selectPage(page, wrapper);
    }

    public List<CourseLessonEntity> list(CourseLessonEntity entity) {
        QueryWrapper<CourseLessonEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getSubjectId())){
            wrapper.eq("subject_id",entity.getSubjectId());
            }
            if (StringUtils.isNotBlank(entity.getTitle())){
            wrapper.eq("title",entity.getTitle());
            }
            if (StringUtils.isNotBlank(entity.getContent())){
            wrapper.eq("content",entity.getContent());
            }
            if (StringUtils.isNotBlank(entity.getCoverUrl())){
            wrapper.eq("cover_url",entity.getCoverUrl());
            }
            if (StringUtils.isNotBlank(entity.getTeacherId())){
            wrapper.eq("teacher_id",entity.getTeacherId());
            }
            if (StringUtils.isNotBlank(entity.getType())){
            wrapper.eq("type",entity.getType());
            }
            if (entity.getLiveDate()!=null){
                wrapper.eq("live_date",entity.getLiveDate());
            }
            if (entity.getScore()!=null){
                wrapper.eq("score",entity.getScore());
            }
            if (entity.getScorePeople()!=null){
                wrapper.eq("score_people",entity.getScorePeople());
            }
        return courseLessonMapper.selectList(wrapper);
    }

}